package org.ahuanlien.crm.service.impl;

import org.ahuanlien.crm.domain.Attendance;
import org.ahuanlien.crm.domain.Department;
import org.ahuanlien.crm.domain.Employee;
import org.ahuanlien.crm.mapper.AttendanceMapper;
import org.ahuanlien.crm.mapper.DepartmentMapper;
import org.ahuanlien.crm.query.StaffManagementQueryObject;
import org.ahuanlien.crm.service.IAttendanceService;
import org.ahuanlien.crm.util.PageResult;
import org.ahuanlien.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class AttendanceServiceImpl implements IAttendanceService {

    @Autowired
    AttendanceMapper attendanceMapper;

    @Autowired
    DepartmentMapper departmentMapper;


    public void save() {
        Attendance attendance = new Attendance();
        //获取当前登录的用户
        Employee employee = UserContext.getCurrentUser();
        attendance.setUsername(employee.getUsername());
        attendance.setDate(new Date());
        attendance.setClockIn(new Date());
        attendance.setClockStatus(true);
        attendanceMapper.insert(attendance);
    }

    public PageResult query(StaffManagementQueryObject qo) {
        int count = attendanceMapper.query4Count(qo);
        if (count == 0) {
            return new PageResult(count, Collections.EMPTY_LIST);
        }
        List<Attendance> result = attendanceMapper.query4List(qo);
        return new PageResult(count, result);
    }


    public void outCard() {
        Employee currentUser = UserContext.getCurrentUser();
        List<Attendance> allDate = attendanceMapper.getAttendByEmpId(currentUser.getUsername());
        Long id = 0L;
        for (Attendance attendance : allDate) {
            if (attendance.getId() > id) {
                id = attendance.getId();
            }
        }
        attendanceMapper.singout(id,new Date());
    }

}
